package com.m3839.sdk.anti.ui.controller;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.text.Html;
import android.text.TextUtils;
import android.view.Gravity;
import android.widget.ImageView;
import android.widget.TextView;

import com.m3839.sdk.anti.bean.LinkBean;
import com.m3839.sdk.anti.bean.PopConfigBean;
import com.m3839.sdk.anti.ui.AntiDialogFragment;
import com.m3839.sdk.anti.ui.RealNameDialogFragment;
import com.m3839.sdk.common.base.UIStackManager;
import com.m3839.sdk.common.core.Constant;
import com.m3839.sdk.common.core.SdkConfiguration;
import com.m3839.sdk.common.ui.NetTipDialogFragment;
import com.m3839.sdk.common.util.AppUtils;
import com.m3839.sdk.common.util.LogUtils;
import com.m3839.sdk.common.util.ResourcesUtils;
import com.m3839.sdk.common.util.ScreenUtils;
import com.m3839.sdk.common.widget.floatview.HykbFloatView;
import com.m3839.sdk.common.widget.floatview.OnClickListener;

import java.util.List;

/**
 * @author ChenBaoyang
 * @description: AntiUiControllerImpl
 * @date 2021/12/8 16:43
 */
public class AntiUiControllerImpl implements IAntiUiController {

    private AntiDialogFragment antiDialogFragment;

    private HykbFloatView hykbFloatView;

    /**
     * 悬浮气泡用到的资源ID
     */
    private final int bubbleLayoutId;
    private final int bubbleTipViewId;
    private final int bubbleDetailViewId;
    private final int bubbleCloseViewId;

    public AntiUiControllerImpl() {
        Context context = SdkConfiguration.getAppContext();
        bubbleLayoutId = ResourcesUtils.getLayoutId(context, "anti_float_bubble");
        bubbleTipViewId = ResourcesUtils.getId(context, "tv_tip");
        bubbleDetailViewId = ResourcesUtils.getId(context, "tv_detail");
        bubbleCloseViewId = ResourcesUtils.getId(context, "iv_close");
    }

    @Override
    public void showFcmPop(Activity activity, List<PopConfigBean> beans,
                               String nick, String uid, String loginType) {

        LogUtils.i("m3839", "beans.size = " + beans.size());
        if (beans.size() == 1) {
            final PopConfigBean bean = beans.get(0);
            if (bean != null && bean.type == 1) {
                //表示弹窗
                showAntiDialog(activity, beans, nick, uid, loginType);
            } else if (bean != null && bean.type == 2) {
                //表示气泡
                showFloatBubble(activity, bean.content, bean.linkBean);
            }
        } else if (beans.size() == 2 && (beans.get(0).type == 1 || beans.get(1).type == 1)) {
            showAntiDialog(activity, beans, nick, uid, loginType);
        }

    }

    private void showAntiDialog(Activity activity, List<PopConfigBean> beans,
                                String nick, String uid, String loginType) {
        dismissFloatBubble();

        if (AppUtils.isInvalidActivity(activity)) {
            return;
        }

        if (antiDialogFragment != null && antiDialogFragment.isVisible()) {
            antiDialogFragment.update(beans, nick, uid, loginType);
            return;
        }

        antiDialogFragment = AntiDialogFragment.newInstance(beans, nick, uid, loginType);
        if (!antiDialogFragment.isVisible()) {
            antiDialogFragment.show(activity.getFragmentManager(), Constant.HYKB_TAG_FRAGMENT_ANTI);
        }
    }

    @Override
    public void updateAntiDialog(int beforeStart) {
        if (antiDialogFragment != null && antiDialogFragment.isVisible()) {
            antiDialogFragment.update(beforeStart);
        }
    }

    @Override
    public void showRealNameDialog(Activity activity, String uid, String loginType, String token) {
        if (UIStackManager.getInstance().hasFragment(Constant.HYKB_TAG_FRAGMENT_ANTI_ENT_TIP)) {
            return;
        }
        RealNameDialogFragment.newInstance(uid, loginType, token).show(activity.getFragmentManager(),
                Constant.HYKB_TAG_FRAGMENT_ANTI_REAL_NAME);
    }

    @Override
    public void showNetTipDialog(Activity activity) {
        if (UIStackManager.getInstance().hasFragment(Constant.HYKB_TAG_FRAGMENT_ANTI_ENT_TIP)) {
            return;
        }
        NetTipDialogFragment.newInstance().show(activity.getFragmentManager(),
                Constant.HYKB_TAG_FRAGMENT_ANTI_ENT_TIP);
    }

    @Override
    public void showFloatBubble(Activity activity, String tip, LinkBean linkBean) {
        if (hykbFloatView != null) {
            hykbFloatView.setText(bubbleTipViewId, Html.fromHtml(tip));
            hykbFloatView.setText(bubbleDetailViewId, linkBean != null ? linkBean.name : "");
            hykbFloatView.show();
            return;
        }
        hykbFloatView = new HykbFloatView(activity)
                .setDuration(3000)
                .setView(bubbleLayoutId)
                .setGravity(Gravity.TOP | Gravity.START)
                .setYOffset(ScreenUtils.dip2px(activity, 8))
                .setText(bubbleTipViewId, Html.fromHtml(tip))
                .setText(bubbleDetailViewId, linkBean != null ? linkBean.name : "")
                .setOnClickListener(bubbleCloseViewId,
                        new OnClickListener<ImageView>() {

                            @Override
                            public void onClick(final HykbFloatView floatView, ImageView view) {
                                floatView.cancel();
                                hykbFloatView = null;
                            }
                        })
                .setOnClickListener(bubbleDetailViewId,
                        new OnClickListener<TextView>() {

                            @Override
                            public void onClick(final HykbFloatView floatView, TextView view) {
                                if (linkBean == null || TextUtils.isEmpty(linkBean.url)) {
                                    return;
                                }
                                //用手机浏览器打开点击的链接
                                AppUtils.openUrl(activity, linkBean.url);
                            }
                        })
                .show();
    }

    @Override
    public void dismissAntiDialog() {
        UIStackManager.getInstance().finishFragment(Constant.HYKB_TAG_FRAGMENT_ANTI);
        antiDialogFragment = null;
    }

    @Override
    public void dismissRealDialog() {
        UIStackManager.getInstance().finishFragment(Constant.HYKB_TAG_FRAGMENT_ANTI_REAL_NAME);
    }

    @Override
    public void dismissAllDialog() {
        UIStackManager.getInstance().finishFragment(Constant.HYKB_TAG_FRAGMENT_ANTI);
        UIStackManager.getInstance().finishFragment(Constant.HYKB_TAG_FRAGMENT_ANTI_REAL_NAME);
    }

    @Override
    public void dismissFloatBubble() {
        if (hykbFloatView != null) {
            hykbFloatView.cancel();
        }
    }
}
